<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="订单号"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getRepairBills">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="repairbills" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="warrantyNum" label="保修单号" align="center" sortable>
			</el-table-column>
			<el-table-column prop="costomer" label="保修人" align="center" sortable>
			</el-table-column>
			<el-table-column prop="dueTime" label="到期时间" :formatter="dateFormat" align="center" sortable>
			</el-table-column>
			<el-table-column label="操作" width="230">
				<template scope="scope">
					<el-button type="success" size="small" @click="handleDetails(scope.$index, scope.row)">明细</el-button>
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--分页条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="5" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="保修单号" prop="warrantyNum">
					<el-input v-model="editForm.warrantyNum" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="保修客户" prop="costomer">
					<el-input v-model="editForm.costomer" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="到期时间" prop="dueTime">
					<el-date-picker
							v-model="editForm.dueTime"
							align="right"
							type="date"
							placeholder="选择日期"
							:picker-options="pickerOptions">
					</el-date-picker>
				</el-form-item>
				<!--<el-form-item label="保修内容" prop="repairContext">
					<el-input v-model="editForm.repairContext" auto-complete="off"></el-input>
				</el-form-item>-->
				<!--<el-form-item label="是否解决" prop="solve">
					<el-radio-group v-model="editForm.solve">
						<el-radio class="radio" :label="1">已解决</el-radio>
						<el-radio class="radio" :label="2">未解决</el-radio>
					</el-radio-group>
				</el-form-item>-->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
		<!--新增界面 -->
		<el-dialog title="新增" v-model="addFormVisible" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="保修单号" prop="warrantyNum" >
					<el-input v-model="addForm.warrantyNum" auto-complete="off" disabled="disabled"></el-input>
				</el-form-item>
				<el-form-item label="保修客户" prop="costomer">
					<el-input v-model="addForm.costomer" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="到期时间" prop="dueTime">
					<el-date-picker
							v-model="addForm.dueTime"
							align="right"
							type="date"
							placeholder="选择日期"
							:picker-options="pickerOptions">
					</el-date-picker>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
		<!--明细界面-->
		<el-dialog title="明细" v-model="detailsFormVisible" :visible.sync="detailsFormVisible" :close-on-click-modal="false">
			<el-table :data="detailsTableData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%">
				<!--<el-table-column type="selection" width="30">
				</el-table-column>-->
				<el-table-column type="index" width="40">
				</el-table-column>
				<el-table-column prop="reparillTime" label="处理时间" :formatter="dateFormat" width="120">
				</el-table-column>
				<el-table-column prop="contents" label="处理明细" align="center" style="width: 60%">
				</el-table-column>
				<el-table-column prop="solve" label="是否解决" align="center" sortable  width="100">
					<template scope="scope_solve">
						<div v-if="scope_solve.row.solve==1">
							<el-button size="small" type="success" icon="el-icon-check" circle></el-button>
						</div>
						<div v-else>
							<el-button size="small" type="danger" icon="el-icon-close" circle></el-button>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" width="150">
					<template slot-scope="scope">
						<el-button size="mini" @click="handleEditDetail(scope.$index, scope.row)">编辑</el-button>
						<el-button size="mini" type="danger" @click="deleteDetails(scope.$index, scope.row)">删除</el-button>
					</template>
				</el-table-column>

			</el-table>
			<div slot="footer" class="dialog-footer">
				<el-button type="success" @click.native="handleadddetails" size="medium" style="width: 100%">添加</el-button>
			</div>
		</el-dialog>
		<!--新增明细框-->
		<el-dialog title="新增明细" v-model="addDetailsFormVisible" :visible.sync="addDetailsFormVisible" :close-on-click-modal="false">
			<el-form :model="addDetailsForm" label-width="80px" :rules="addDetailsFormRules" ref="addDetailsForm">
				<el-form-item label="维修内容" prop="contents">
					<el-input v-model="addDetailsForm.contents" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="是否解决" prop="solve">
					<el-radio-group v-model="addDetailsForm.solve">
						<el-radio class="radio" :label="1">已解决</el-radio>
						<el-radio class="radio" :label="2">未解决</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addDetailsFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addDetailsSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
		<!--编辑明细框-->
		<el-dialog title="编辑明细" v-model="editDetailsFormVisible" :visible.sync="editDetailsFormVisible" :close-on-click-modal="false">
			<el-form :model="editDetailsForm" label-width="80px" :rules="editDetailsFormRules" ref="editDetailsForm">
				<el-form-item label="维修内容" prop="contents">
					<el-input v-model="editDetailsForm.contents" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="是否解决" prop="solve">
					<el-radio-group v-model="editDetailsForm.solve">
						<el-radio class="radio" :label="1">已解决</el-radio>
						<el-radio class="radio" :label="2">未解决</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editDetailsFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editDetailsSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>

	export default {
		data() {
			return {
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() < Date.now();
                    }
                },
			    //高级查询的过滤器
				filters: {
					name: ''
				},
				//保存表格中的数据
				repairbills: [],
				//明细表中的数据
                detailsTableData: [],
				//总页数
				total: 0,
				//默认显示条数
				pageSize:5,
				//当前页
				page: 1,
				//加载默认关闭
				listLoading: false,
				//选中的列
				sels: [],
				//默认不显示编辑页面
				editFormVisible: false,
				//默认不显示编辑加载
				editLoading: false,
				//编辑页面的表单验证规则
				editFormRules: {
					name: [
						{ required: true, message: '请输入部门名称', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
					id: 0,
                    warrantyNum: '',
                    costomer: '',
                    dueTime: new Date()
				},
                //新增界面是否显示
				addFormVisible: false,
				//加载
				addLoading: false,
				//验证规则
				addFormRules: {
					name: [
						{ required: true, message: '请输入部门名称', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
                    warrantyNum: '',
                    costomer: '',
                    dueTime: new Date()
				},
                //默认不显示明细页面
				detailsFormVisible: false,
                //默认不显示明细加载
                detailsLoading: false,
                //明细页面的表单验证规则
                detailsFormRules: {
                    name: [
                        { required: true, message: '请输入部门名称', trigger: 'blur' }
                    ]
                },

				//---------------------------------------
                addDetailsForm:{
                    contents:'',
                    solve:'',
                    warrantyId:''
				},
				signWarrantyID:'',
                editDetailsForm:{
                    id:'',
                    contents:'',
                    solve:'',
                    warrantyId:''
                },
				//默认不显示明细添加框
                addDetailsFormVisible: false,
                //默认不显示明细修改框
                editDetailsFormVisible: false,
                addDetailsFormRules: {
                },
				editDetailsFormRules: {
                },
			}
		},
		methods: {
			//处理分页问题
			handleCurrentChange(val) {
			    //当前页赋值
				this.page = val;
				//获取数据
				this.getRepairBills();
			},
			//获取列表
			getRepairBills() {
			    //分页参数
				let para = {
                    currentPage: this.page,
                    pageSize:this.pageSize,
					keyword: this.filters.name
				};
				//console.log(para)
				//加载
				this.listLoading = true;
				//getUserListPage(para).then((res) => {
				this.$http.patch("/warranty/page",para).then((res)=>{
				    //console.log(res);
				    //总页数
					this.total = res.data.total;
					this.repairbills = res.data.rows;
					this.listLoading = false;
				});
			},
            //获取明细列表
            getDetails() {
                //加载
                this.listLoading = true;
                this.$http.patch("/warrantyDetails/details",{warrantyID:this.signWarrantyID}).then((res)=>{
                    this.detailsTableData = res.data;
                    this.listLoading = false;
                });
            },
            //显示编辑界面  index:修改那行索引  row 修改那行的数据
            handleEdit: function (index, row) {
                //打开修改对话框
                this.editFormVisible = true;
                //把表格那行数据 赋值给 this.editForm
                this.editForm = Object.assign({}, row);
                //console.log(this.editForm.solve);
            },
            //显示新增界面
            handleAdd: function () {
                //产生单号
                let d = new Date().getTime();
                var num = '';
                for (var i = 0; i < 3; i++) {
                    num += Math.floor(Math.random() * 10);
                }
                num = d + num;
				//console.debug(d);
				//清空表单的值
                this.addForm = {
                    warrantyNum: num,
                    costomer: '',
                    dueTime: null
                };
                //显示对话框
                this.addFormVisible = true;
                //调用查询下拉
                //this.getEmployees();
            },
			//显示明细页面
            handleDetails: function (index, row) {
                //打开修改对话框
                this.detailsFormVisible = true;
                //加载
                this.listLoading = true;
                //先赋值，记录
                this.signWarrantyID = row.id;
                this.$http.patch("/warrantyDetails/details",{warrantyID:row.id}).then((res)=>{
                    //todo
                    this.detailsTableData = res.data;
                    this.listLoading = false;
                });
            },
            //新增
            addSubmit: function () {
                //新增之前的验证功能
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //加载
                            this.addLoading = true;
                            let para = Object.assign({}, this.addForm);
                            this.$http.put('/warranty',para).then(res=>{
                                this.addLoading = false;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                //验证的重置
                                this.$refs['addForm'].resetFields();
                                //关闭新增对话框
                                this.addFormVisible = false;
                                this.getRepairBills();
                            });
                        });
                    }
                });
            },
            //编辑保存
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //para 这个参数 就有id 和name这个值
                            let para = Object.assign({}, this.editForm);
                            this.$http.post('/warranty',para).then(res=>{
                                this.editLoading = false;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                //关闭对话框
                                this.editFormVisible = false;
                                this.getRepairBills();
                            });
                        });
                    }
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.delete('/warranty/'+row.id).then((res) => {
                        this.listLoading = false;
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getRepairBills();
                    });
                }).catch(() => {

                });
            },
			//添加明细弹出框
            handleadddetails: function () {
                //清空表单的值
                this.addDetailsForm = {
                    contents:'',
                    solve:'',
                };
                //显示对话框
                this.addDetailsFormVisible = true;
            },
			//编辑明细弹出框
            handleEditDetail: function (index, row) {
                //显示对话框
                this.editDetailsFormVisible = true;
                //把表格那行数据 赋值给 this.editDetailsForm
                this.editDetailsForm = Object.assign({}, row);
            },
            //编辑明细保存
            editDetailsSubmit: function () {
                this.$refs.editDetailsForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //para 这个参数 就有id 和name这个值
                            this.editDetailsForm.warrantyId = this.signWarrantyID;
                            let para = Object.assign({}, this.editDetailsForm);
                            this.$http.post('/warrantyDetails',para).then(res=>{
                                this.editLoading = false;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                //关闭对话框
                                this.editDetailsFormVisible = false;
                                this.getDetails();
                            });
                        });
                    }
                });
            },
			//新增明细
            addDetailsSubmit: function () {
                //新增之前的验证功能
                this.$refs.addDetailsForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //加载
                            this.addLoading = true;
                            this.addDetailsForm.warrantyId = this.signWarrantyID;
                            let para = Object.assign({}, this.addDetailsForm);
                            //todo
							console.debug(para);
                            this.$http.put('/warrantyDetails',para).then(res=>{
                                this.addLoading = false;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                //验证的重置
                                this.$refs['addDetailsForm'].resetFields();
                                //关闭明细新增对话框
                                this.addDetailsFormVisible = false;
                                this.getDetails();
                            });
                        });
                    }
                });
            },
			//删除明细
            deleteDetails: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.delete('/warrantyDetails/'+row.id).then((res) => {
                        this.listLoading = false;
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.detailsFormVisible = true;
                        this.getDetails();
                    });
                }).catch(() => {
                });
            },





			//多选的方法
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
                //console.log(ids);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
                    this.$http.post('/warranty/deleteMany',para).then(res=>{
                        this.editLoading = false;
                        this.$message({
                            message: '提交成功',
                            type: 'success'
                        });
                        //关闭对话框
                        //this.editFormVisible = false;
                        this.getRepairBills();
                    });
				}).catch(() => {

				});
			},
            solveFormat(row, column, cellValue, index){
			    if(cellValue == 1){
                    return "已解决";
				}else {
                    return "未解决";
				}
            },
            dateFormat(row, column, cellValue, index){
                if(cellValue==null || cellValue=="") return "";
                let date = new Date(parseInt(cellValue));
                let Y = date.getFullYear() + '-';
                let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-';
                let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
                let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
                let m = date.getMinutes()  < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
                let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
                return Y + M + D ;
            }
		},
		mounted() {
			this.getRepairBills();
		}
	}

</script>

<style scoped>

</style>